<template>
	<view class="tag-select" v-if="show">
		<view class="mark" @touchmove.stop="" @click="show=false"></view>
		<view class="content">
			<view class="content-head"><view @click="show=false" class="content-head-touch"></view></view>
			<view class="content-title">
				<view class="title" v-if="title">{{title}}</view>
				<view class="title-d" v-if="dtitle">{{dtitle}}</view>
			</view>
			<view class="content-tags">
				<view v-if="index>0" :class="['tag',item.checked?'tag-act':'']" @click="chooseItem(item,index)" v-for="(item,index) in tags" :key='index'>
					{{item[name]}}
				</view>
			</view>
			<view class="content-btn"><view class="btn" @click="success">确定</view></view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'tag-select',
	data() {
		return {
			keyval:'',
			title:'指标设置',
			dtitle:'至少选择1个选项',
			show: false,
			tags:[{
				name:'待开始',
				checked:false
			},{
				name:'待开始占比',
				checked:false
			},{
				name:'进行中',
				checked:false
			},{
				name:'已提交',
				checked:false
			},{
				name:'已提交',
				checked:false
			},{
				name:'已提交',
				checked:false
			},{
				name:'已提交',
				checked:false
			}], 
			name:'name', 
		};
	},
	methods: {
		chooseItem(item,index) {
			item.checked = !item.checked; 
			console.log(this.tags);
		},
		init(data) {
			this.tags = JSON.parse(JSON.stringify(data.tags || []));
			this.tags.forEach(item=>{
				item.name = item.name.replaceAll('/','');
				item.name = item.name.replaceAll('(小时)','');
			});
			this.keyval = data.key;
			if (data.name) {
				this.name = data.name;
			}
			if (data.title) {
				this.title = data.title;
			}
			if (data.dtitle) {
				this.dtitle = data.dtitle;
			}
			this.show = true;
		},
		success() {
			let select = false;
			let checked = 0;
			for (let i = 0; i < this.tags.length; i++) {
				if (this.tags[i].checked ) {
					if (checked > 0) {
						select = true;
						break;
					}
					checked++;
				}
			}
			if (!select) {
				uni.showToast({
					title:'至少选择一项',
					icon:'none'
				});
				return;
			}
			this.$emit('success',{tags:this.tags,key:this.keyval});
			this.show = false;
		}
	}
};
</script>

<style scoped lang="scss">
.tag-select {
	width: 100%;
	position: fixed;
	top: 0;
	left: 0;
	z-index: 99;
	.mark {
		position: fixed;
		z-index: 99;
		background: rgba(0, 0, 0, 0.17);
		top: 0;
		left: 0;
		width: 100%;
		height: 100vh;
	}

	.content {
		z-index: 100;
		position: fixed;
		bottom: 0;
		left: 0;
		height: 600rpx;
		border-radius: 8rpx 8rpx 0rpx 0rpx;
		overflow: hidden;
		width: 100%;
		box-shadow: 0rpx -4rpx 12rpx 0rpx rgba(0, 0, 0, 0.12);
		background-color: #fff;
		animation: upm 0.3s;
		display: flex;
		flex-direction: column;
		&-head{
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: center; 
			height: 36rpx;
			&-touch{
				width: 64rpx;
				height: 10rpx;
				background: #d8d8d8;
				border-radius: 6rpx;
			}
		}
		
		&-title{
			width: calc(100% - 80rpx); 
			line-height: 39rpx;
			padding: 20rpx 40rpx;
			.title{
				font-size: 32rpx;
				font-weight: bold;
				color: #333;
			}
			.title-d{
				color: #999999;
				font-size: 24rpx;
			}
		}
		&-tags{
			flex: 1;
			overflow-y: scroll; 
			width: calc(100% - 80rpx);
			padding: 0 40rpx; 
			line-height: 67rpx;
			.tag{
				font-size: 28rpx;
				color: #666666;
				border: 2rpx solid #cccccc;
				height: 48rpx;
				text-align: center;
				display: inline-block;
				border-radius: 8rpx;
				line-height: 47rpx;
				padding: 0 22rpx;
				margin-right: 16rpx;
				transition: .1s all;
			}
			
			.tag-act{
				color: #0091ff;
				border-color: #0091ff;
			}
		}
		&-btn{
			width: 100%;
			.btn{
				width: 670rpx;
				height: 70rpx;
				background: #0091ff;
				border-radius: 8rpx;
				text-align: center;
				line-height: 69rpx;
				font-size: 28rpx;
				color: #fff;
				margin: 32rpx auto;
			}
		}
	}
}

@keyframes upm {
	0% {
		transform: translateY(870rpx);
	}

	100% {
		transform: translateY(0);
	}
}
</style>
